broadway: Ensure images are decoded, not only loaded
authorAlexander Larsson <alexl@redhat.com>
Fri, 13 Mar 2020 07:56:56 +0000 (08:56 +0100)
committerAlexander Larsson <alexl@redhat.com>
Fri, 13 Mar 2020 14:00:24 +0000 (15:00 +0100)
In firefox, onload will trigger when the image is loaded, but at
that point it may not be decoded yet so showing it will sometimers
trigger flashes. We use the new decode() feature instead which ensures
both that the image is loaded *and* decoded, thus fixing the flashes.

gdk/broadway/broadway.js

index 2478aacc7c70b1fead39dc3fc9a37d87a6da4dcf..5b8311427c6a7f1d03fc770da494dbf3b45bd44e 100644 (file)
@@ -274,6 +274,7 @@ function Texture(id, data) {
     var image = new Image();
     image.src = this.url;
     this.image = image;
+    this.decoded = image.decode();
     textures[id] = this;
 }
 
@@ -1237,16 +1238,14 @@ function handleOutstanding()
         outstandingDisplayCommands = display_commands;
 
     if (new_textures.length > 0) {
-        var n_textures = new_textures.length;
+        var decodes = [];
         for (var i = 0; i < new_textures.length; i++) {
-            var t = new_textures[i];
-            t.image.onload = function() {
-                n_textures -= 1;
-                if (n_textures == 0) {
-                    handleOutstandingDisplayCommands();
-                }
-            };
+            decodes.push(new_textures[i].decoded);
         }
+        Promise.allSettled(decodes).then(
+            () => {
+                handleOutstandingDisplayCommands();
+            });
     } else {
         handleOutstandingDisplayCommands();
     }